package cn.tedu._05weibo.common.execption;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobolExecptionHandler {

    @ExceptionHandler
    public JsonResult doHandleIllegalArgumentException(IllegalArgumentException ex){
         log.error("ex="+ex.getMessage());
         return  new JsonResult(StatusCode.OPERATION_FAILED,ex.getMessage());
    }

//    @ExceptionHandler
//    public JsonResult doHandleRuntimeExection(RuntimeException ex){
//        log.error("ex="+ex.getMessage());
//        return  new JsonResult(StatusCode.OPERATION_FAILED, ex.getMessage());
//    }
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){

        String message=ex.getFieldError().getDefaultMessage();
        log.error("message="+message);
        return  new JsonResult(StatusCode.VALIDATED_ERROR, message);
    }
//    @ExceptionHandler
//    public JsonResult doThrowableException(Throwable ex){
//        log.error("ex="+ex.getMessage());
//        return  new JsonResult(StatusCode.THROWABLE_ERROR, ex.getMessage());
//    }

    @ExceptionHandler
    public JsonResult doConstraintViolationException(ConstraintViolationException ex){
        String message=ex.getMessage().split(":")[1].trim();
        log.error("ConstraintViolationException="+message);
        return  new JsonResult(StatusCode.VALIDATED_ERROR, message);
    }

}
